<?php
/**
 * Created by xun at 2021/4/26
 */

namespace console\controllers;

use common\models\c2\entity\Collection;
use common\models\c2\entity\Order;
use common\models\c2\entity\Venue;
use common\models\c2\statics\CollectionStatus;
use common\models\c2\statics\EntityDeleteStatus;
use common\models\c2\statics\OrderStatus;
use common\models\c2\statics\VenueStatus;
use cza\base\models\statics\EntityModelState;
use yii\console\Controller;
use yii\db\Exception;
use yii\helpers\ArrayHelper;

class OrderController extends Controller
{

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
    }

    public function actionIndex()
    {
        $orders = Order::find()
            ->with('collection')
            ->where(['status' => OrderStatus::STATUS_DEFAULT])
            ->all();

        $time = time();
        foreach ($orders as $order) {

            // print_r("现在时间:" . $time . "\n\r");
            // print_r("开放时间:" . strtotime($venue->open_at) . "\n\r");
            // print_r("关闭时间:" . strtotime($venue->close_at) . "\n\r");

            if (($time - $order->order_time) >= 3600) {
                \Yii::info('关闭订单：' . $order->id . "\n\r");
                $order->updateAttributes(['status' => OrderStatus::STATUS_CLOSE]);
                $order->collection->updateAttributes(['status' => CollectionStatus::STATUS_DEFAULT]);
            } else {
                $order->collection->updateAttributes(['status' => CollectionStatus::STATUS_SOLD_OUT]);
            }
        }
    }

}